package com.apes.scm.masterdata.area.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: BusinessCircle
 * @Description: 定义商圈信息表
 * @author: ZhengZeXian
 * @create: 2022/2/16 16:32
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BusinessCircle.CONST_TABLE_NAME)
public class BusinessCircle extends Model {

    public static final String CONST_TABLE_NAME = "mas_business_circle";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商圈信息",increment = 10,numberNext = 140, padding = 3)
    @Comment("编码")
    private String id;

    @Column(name = "name", length = 64)
    @Comment("名称")
    private String name;

    @Column(name = "remark", length = 250)
    @Comment("备注")
    private String remark;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("是否有效")
    private boolean valid = true; //有效

    @OneToMany(mappedBy = "businessCircle", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<BusinessCircleArea> businessCircleAreas = new ArrayList();

    public List<BusinessCircleArea> getBusinessCircleAreas() {
        if (businessCircleAreas == null) return null;
        for (BusinessCircleArea item : businessCircleAreas) {
            item.setBusinessCircle(this);
        }
        return businessCircleAreas;
    }

    public void setBusinessCircleAreas(List<BusinessCircleArea> businessCircleAreas) {
        if (businessCircleAreas == null) businessCircleAreas = new ArrayList();
        this.businessCircleAreas.clear();
        this.businessCircleAreas.addAll(businessCircleAreas);
    }
}

